<?php
/**
 * 微软相关
 */
namespace app\common\lib;

class Azure
{
    /**
     *
     * 语音合成
     * @param $role // 角色
     * @param $txt // 合成文本
     * @param $fileName // 文件路径
     * @return false|mixed|void
     */
    public static function tts($role,$txt,$fileName){
        try{
            $AccessTokenUri = "https://southeastasia.api.cognitive.microsoft.com/sts/v1.0/issuetoken";
            $apiKey = "b6d48e9489454abba501238459ecf102";
            $options = array(
                'http' => array(
                    'header'  => "Ocp-Apim-Subscription-Key: ".$apiKey."\r\n" .
                        "content-length: 0\r\n",
                    'method'  => 'POST',
                ),
            );
            $context  = stream_context_create($options);
            $access_token = file_get_contents($AccessTokenUri, false, $context);

            if (!$access_token) {
                exception("Problem with $AccessTokenUri, $php_errormsg");
                return false;
            }
            else{
                $ttsServiceUri = "https://southeastasia.tts.speech.microsoft.com/cognitiveservices/v1";

                $doc = new \DOMDocument();

                $root = $doc->createElement( "speak" );
                $root->setAttribute( "version" , "1.0" );
                $root->setAttribute( "xml:lang" , "zh-CN" );

                $voice = $doc->createElement( "voice" );
                $voice->setAttribute( "xml:lang" , "zh-CN" );
                $voice->setAttribute( "xml:gender" , "Male" );
                $voice->setAttribute( "name" , $role); // Short name for "Microsoft Server Speech Text to Speech Voice (en-US, Guy24KRUS)"

                $text = $doc->createTextNode( $txt);

                $voice->appendChild( $text );
                $root->appendChild( $voice );
                $doc->appendChild( $root );
                $data = $doc->saveXML();
                $options = array(
                    'http' => array(
                        'header'  => "Content-type: application/ssml+xml\r\n" .
                            "X-Microsoft-OutputFormat: riff-24khz-16bit-mono-pcm\r\n" .
                            "Authorization: "."Bearer ".$access_token."\r\n" .
                            "X-Search-AppId: 07D3234E49CE426DAA29772419F436CA\r\n" .
                            "X-Search-ClientID: 1ECFAE91408841A480F00935DC390960\r\n" .
                            "User-Agent: TTSPHP\r\n" .
                            "content-length: ".strlen($data)."\r\n",
                        'method'  => 'POST',
                        'content' => $data,
                    ),
                );

                $context  = stream_context_create($options);

                // get the wave data
                $result = file_get_contents($ttsServiceUri, false, $context);
                if (!$result) {
                    exception("Problem with $AccessTokenUri, $php_errormsg");
                    return false;
                }
                else{
                    $res = file_put_contents($fileName, $result);
                    if($res){
                        return $result;
                    }else{
                        return false;
                    }
                }
            }
        }catch (\Exception $e){
            exception($e,'Azure.tts');
        }
    }
}